var path = require('path');
var webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
  entry: [
    'webpack-hot-middleware/client?reload=true',
    'react-hot-loader/patch',
    path.join(__dirname, '../lib/index.js'),
  ],

  output: {
    filename: 'app.js',
    path: path.resolve(__dirname, '../public'),
    publicPath: '/',
  },

  devtool: 'source-map',

  module: {
    rules: [
      { 
        test: /\.js$/,
        use: [
          'react-hot-loader/webpack',
          'babel-loader'
        ],
        exclude: [path.resolve(__dirname, '../node_modules')]
      },
      {
        test: /\.(eot|ttf|woff|woff2|svg)$/,
        use: [
          {
            loader: 'file-loader',
            options: {
              name: 'fonts/[name].[ext]',
              publicPath: '../'
            }
          }
        ]
      },
      {
        test: /\.(png|jpg|jpeg)$/,
        use: [
          {
            loader: 'file-loader',
            options: {
              name: 'images/[name].[ext]',
              publicPath: '../'
            }
          }
        ]
      },
      {
        test: /\.scss$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: [
            {
              loader: 'css-loader',
              options: {
                modules: true,
                sourceMap: true,
                localIdentName: '[name]__[local]___[hash:base64:5]',
              }
            },
            {
              loader: 'postcss-loader',
              options: {
                sourceMap: true,
                plugins: [
                  require('autoprefixer')({
                    browsers: [
                      'last 2 versions',
                      'IE >= 9',
                      'safari >= 8',
                      'Firefox >= 20'
                    ]
                  }),
                ]
              }
            },
            {
              loader: 'sass-loader',
              options: {
                sourceMap: true,
                outputStyle: 'expanded',
                includePaths: [
                  path.resolve(__dirname, '../assets/sass')
                ]
              }
            },
          ],
        })
      },
    ]
  },

  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoEmitOnErrorsPlugin(),
    new ExtractTextPlugin('styles/main.css'),
    // new webpack.NamedModulesPlugin(),
    // 在浏览器控制台中在HMR更新中打印更多可读的模块名称
  ],
};
